/******************************************************************************

  Copyright (C), 2011-2013, 广州海特天高信息系统工程有限公司

 ******************************************************************************
  File Name     : kb_def.h
  Version       : Initial Draft
  Author        : 研发中心软件组
  Created       : 2012/06/05
  Description   : 主控键盘程序的数据定义
  History       :
  1.Date        : 
    Author      : 
    Modification:     
******************************************************************************/  


#ifndef __KB_DEF_H__
#define __KB_DEF_H__


#if !defined(WIN32)
	#define __PACKED__        __attribute__ ((__packed__))
#else
	#define __PACKED__ 
#endif

#if !defined(WIN32)
	#include "win_types.h"
#endif

#define PTZ_MATRIX_SWITCH	100

//#define begin
#define MAX_CHAN 2				//最大视频通道数
#define MAX_CHAN 2				//最大视频通道数
#define VENC_MAX_CHAN 2			//最大的编码通道个数
#define VDEC_MAX_CHAN 1			//最大的解码通道个数
#define IP_ADDR_LEN                15

#define MAKE_MAGIC_CODE(a,b,c,d)   (a | (b<<8) | (c<<16) | (d<<24))
#define SYSCFG_MAGIC_CODE          MAKE_MAGIC_CODE('A', 'E', 'B', 'C') // Config file magic code
#define NETWORK_MAGIC_CODE         MAKE_MAGIC_CODE('A', 'E', 'B', 'N') // Network command magic code
#define MAX_TRANSPACK_SIZE         8192 	//网络包最大长度 8192
#define KEY_BORAD_PORT             1026 	//键盘通讯的TCP端口
#define USER_NAME_LEN              16
#define USER_PSW_LEN               16

//设备类型
typedef enum
{
	DEV_IPC,  // IP Camera
	DEV_DVS,  // Video Server
	DEV_DVR,  // Video Recorder
	DEV_MAX		// Maxtrix（Video Decoder）
}enDeviceType;

//通信协议数据包类型
typedef enum
{
	// Client Request Command,   Client -> Server
	CMD_ReqLogin           = 1000,
	CMD_ReqLogout          = 1999, 	// No Answer
	CMD_ReqNetClick 	   = 1998,	//发送网络心跳数据(liyz add)
	CMD_ReqMon             = 1001, 	//打开或关闭主码流
	CMD_ReqGetChanSet      = 1002,	//读取通道参数
	CMD_ReqSetChanSet      = 1003,	//设置通道参数
	CMD_ReqGetCmmSet       = 1004,	//读取基本参数
	CMD_ReqSetCmmSet       = 1005,	//设置基本参数
	CMD_ReqPtzAct          = 1006,	//PTZ控制
	CMD_ReqManualAlarm     = 1007,	//手动报警(设备软件未实现)
	CMD_ReqUpdateFile      = 1008,	//更新文件(设备软件未实现)
	CMD_ReqDlRecFile       = 1009,	//查询D1录像文件(设备软件未实现)
	CMD_ReqStopPostFile    = 1010,	//停止上传文件(设备软件未实现)
	CMD_ReqGetUserList     = 1011,	//读取用户信息列表
	CMD_ReqModifyUser      = 1012,	//修改用户信息
	CMD_ReqSysLog          = 1013,	//查询系统日志(设备软件未实现)
	CMD_ReqGetDevInfo      = 1014,	//读取设备信息
	CMD_ReqRebootDevice    = 1015,	//重启设备
	CMD_ReqSetDevClock     = 1016,	//设置设备时钟
	CMD_ReqRestoreFactory  = 1017,	//恢复出厂设置(设备软件未实现)
	CMD_ReqSetFontMatrix   = 1018,	//设置字库点阵(设备软件未实现)
	CMD_ReqIFrame          = 1019,	//请求I帧
	CMD_ReqPtzPrtclInfo    = 1020,	//查询PTZ控制参数(设备软件未实现)
	CMD_ReqSendComData     = 1021,	//要求发送串口数据(设备软件未实现)
	CMD_ReqSrchRec         = 1022,	//查询录像文件(设备软件未实现)
	CMD_ReqStartPb         = 1023,	//开始回放录像(设备软件未实现)
	CMD_ReqSetPbSpeed      = 1024,	//设置回放速度(设备软件未实现)
	CMD_ReqStopPb          = 1025,	//停止回放(设备软件未实现)
	CMD_ReqSetPbPos        = 1026,	//设置回放位置(设备软件未实现)
	CMD_ReqTalkback        = 1027,	//开始或结束语音对讲
	CMD_ReqStorageInfo     = 1028,	//查询磁盘信息(设备软件未实现)
	CMD_ReqFormatDisk      = 1029,	//格式化磁盘(设备软件未实现)
	CMD_ReqDiskDirInfo     = 1030,	//查询磁盘目录信息(设备软件未实现)
	CMD_ReqPrevVidEffect   = 1031,
	CMD_ReqMonSub          = 1032,	//打开或关闭子码流
	CMD_ReqGetCmmSetEx     = 1033,	//查询扩展的基本参数(设备软件未实现)
	CMD_ReqSetCmmSetEx     = 1034,	//设置扩展的基本参数(设备软件未实现)
	CMD_ReqSnapshot        = 1035,	//快照(设备软件未实现)
	CMD_ReqGetZcCfg        = 1036,	//查询镜头信息(设备软件未实现)
	
	CMD_ReqSerialStart     = 1037,	//建立透明通道
	CMD_ReqSerialSend	     = 1038,	//通过透明通道向设备串口发送数据
	CMD_ReqSerialStop	     = 1039,	//断开透明通道		
	CMD_ReqDecodeSwitch	   = 1040,	//解码切换	
	CMD_ReqIOControl  	   = 1041,	//报警IO量控制
	CMD_ReqIOState  	     = 1042,	//查询报警IO量状态
	
	CMD_ReqMonEx             = 1043, 	//打开或关闭主码流(扩展功能)
	CMD_ReqMonSubEx          = 1044,	//打开或关闭子码流(扩展功能)
	
	CMD_ReqGetAudioSet      = 1045,	//读取音频参数
	CMD_ReqSetAudioSet      = 1046,	//设置音频参数		
	CMD_ReqGetVideoEncSet      = 1047,	//读取视频编码参数
	CMD_ReqSetVideoEncSet      = 1048,	//设置视频编码参数


	// Server Answer Command,    Server -> Client
	CMD_AswLogin           = 2000,
	CMD_AswMon             = 2001,
	CMD_AswGetChanSet      = 2002,
	CMD_AswSetChanSet      = 2003,
	CMD_AswGetCmmSet       = 2004,
	CMD_AswSetCmmSet       = 2005,
	CMD_AswPtzAct          = 2006,
	CMD_AswManualAlarm     = 2007,	//	手动报警请求的应答
	CMD_AswUpdateFile      = 2008,
	CMD_AswDlRecFile       = 2009,
	CMD_AswStopPostFile    = 2010,
	CMD_AswGetUserList     = 2011,
	CMD_AswModifyUser      = 2012,
	CMD_AswSysLog          = 2013,
	CMD_AswGetDevInfo      = 2014,
	CMD_AswRebootDevice    = 2015,
	CMD_AswSetDevClock     = 2016,
	CMD_AswRestoreFactory  = 2017,
	CMD_AswSetFontMatrix   = 2018,
	CMD_AswIFrame          = 2019,
	CMD_AswPtzPrtclInfo    = 2020,
	CMD_AswSendComData     = 2021,
	CMD_AswSrchRec         = 2022,
	CMD_AswStartPb         = 2023,
	CMD_AswSetPbSpeed      = 2024,
	CMD_AswStopPb          = 2025,
	CMD_AswSetPbPos        = 2026,
	CMD_AswTalkback        = 2027,  //开始或结束语音对讲ACK
	CMD_AswStorageInfo     = 2028,
	CMD_AswFormatDisk      = 2029,
	CMD_AswDiskDirInfo     = 2030,
	CMD_AswPrevVidEffect   = 2031,
	CMD_AswMonSub          = 2032,
	CMD_AswGetCmmSetEx     = 2033,
	CMD_AswSetCmmSetEx     = 2034,
	CMD_AswSnapshot        = 2035,
	CMD_AswGetZcCfg        = 2036,
	CMD_AswSerialStart     = 2037,	//liyz add(2011-12-20)
	CMD_AswSerialSend      = 2038,	//liyz add(2011-12-20)
	CMD_AswSerialStop      = 2039,	//liyz add(2011-12-20)
	CMD_AswDecodeSwitch	   = 2040,	//解码切换ACK
	CMD_AswIOControl  	   = 2041,	//报警IO量控制ACK
	CMD_AswIOState  	   = 2042,	//查询报警IO量状态ACK
	CMD_AswMonEx           = 2043,	//打开或关闭主码流的应答(扩展功能)
	CMD_AswMonSubEx        = 2044,  //打开或关闭子码流的应答(扩展功能)
	
	CMD_AswGetAudioSet      = 2045,	//读取音频参数ACK
	CMD_AswSetAudioSet      = 2046,	//设置音频参数ACK		
	CMD_AswGetVideoEncSet      = 2047,	//读取视频编码参数ACK
	CMD_AswSetVideoEncSet      = 2048,	//设置视频编码参数ACK	

	CMD_AswNetClick 	     = 2998,		//发送网络心跳数据
	
	// Post Command,    Server <-> Client
	CMD_PostCmmLog         = 3000, // Server post common log. Common log no need request.
	CMD_PostSysLog         = 3001, // Server post system log. Currently reserved.
	CMD_PostMonData        = 3002, // Server transmit monitor video & audio data to Client.
	CMD_PostAlarmInfo      = 3003, // Server send alarm info to client.
	CMD_PostUpdateFileData = 3004, // Client update front end device's file.
	CMD_PostSrchRec        = 3005, // Search record file result.
	CMD_PostPbData         = 3006, // Server post record playback file data to client.
	CMD_PostPbProg         = 3007, // Server post playback progress to client.
	CMD_PostVoiceDownData  = 3008, // Talkback,从用户到设备的语音对讲数据
	CMD_PostVoiceUpData    = 3009, // Talkback,从设备到用户的语音对讲数据
	CMD_PostUdpMakeHole    = 3010, // UDP make hole, ethernet <-> internet
	CMD_PostRefreshDevInfo = 3011, // When server's device info change, server send new device info to client.
	CMD_PostDiskDirInfo    = 3012, // Server post disk dir info to clent.
	CMD_PostMonSubData     = 3013, // Server transmit sub channel monitor video & audio data to Client.
	CMD_PostDynChangeSet   = 3014, // Server post client that some key settings of channel changed.
	CMD_PostAlarmInfoEx    = 3015,//Server send alarm info to client extent
	CMD_PostSnapshotData   = 3016,
	CMD_PostSerialData     = 3017, //Server send serial data to client.(liyz add 2011-12-20)
	CMD_PostMonDataEx      = 3018, //由设备上传的主码流数据 
	CMD_PostMonSubDataEx   = 3019, //由设备上传的子码流数据 
	
	CMD_PostDeviceInfo   = 3999, //广播设备ID信息
	
}enNetCmd;

//PTZ命令类型
typedef enum
{
	// Up, Down, Left, Right
	PTZ_UP_START = 0,
	PTZ_UP_STOP,
	PTZ_DOWN_START,
	PTZ_DOWN_STOP,
	PTZ_LEFT_START,
	PTZ_LEFT_STOP,
	PTZ_RIGHT_START,
	PTZ_RIGHT_STOP,
	// (Optional) : LeftUp, LeftDown, RightUp, RightDown
	PTZ_LEFT_UP_START,
	PTZ_LEFT_UP_STOP,
	PTZ_LEFT_DOWN_START,
	PTZ_LEFT_DOWN_STOP,
	PTZ_RIGHT_UP_START,
	PTZ_RIGHT_UP_STOP,
	PTZ_RIGHT_DOWN_START,
	PTZ_RIGHT_DOWN_STOP,

	// Focus, Zoom, Iris
	PTZ_FOCUS_FAR_START = 20,
	PTZ_FOCUS_FAR_STOP,
	PTZ_FOCUS_NEAR_START,
	PTZ_FOCUS_NEAR_STOP,
	PTZ_ZOOM_TELE_START,
	PTZ_ZOOM_TELE_STOP,
	PTZ_ZOOM_WIDE_START,
	PTZ_ZOOM_WIDE_STOP,
	PTZ_IRIS_CLOSE_START,
	PTZ_IRIS_CLOSE_STOP,
	PTZ_IRIS_OPEN_START,
	PTZ_IRIS_OPEN_STOP,

	// Rotation, Light, Rain, Track, IO
	PTZ_ROTATION_START = 40, //水平翻转180度
	PTZ_ROTATION_STOP,
	PTZ_LIGHT_START,
	PTZ_LIGHT_STOP,
	PTZ_RAIN_START,
	PTZ_RAIN_STOP,
	PTZ_TRACK_START,
	PTZ_TRACK_STOP,
	PTZ_IO_START,
	PTZ_IO_STOP,
	PTZ_PAN_ZERO,	//水平复位

	// PTZ Point
	PTZ_SET_POINT = 60,	// 设置
	PTZ_SET_POINT_ROTATION,
	PTZ_GOTO_POINT,	// 调用预
	PTZ_CLEAR_POINT,
	PTZ_SET_POINT_LEFT,
	PTZ_GOTO_POINT_LEFT,
	PTZ_SET_POINT_RIGHT,
	PTZ_GOTO_POINT_RIGHT,

	// Mouse click of PTZ moving & zooming
	PTZ_MOUSE_CLICK_MOVE_ZOOM = 80,

	//PTZ Auto Scan
	PTZ_AUTO_SCAN = 100,
	PTZ_AUTO_SCAN_PRESET,   //预置位自动巡视

	//Zoom Camera Action
	ZC_ALL_DEFAULT = 120,
	ZC_WIDEDYNAMIC_OPEN, //wide Dynamic open
	ZC_WIDEDYNAMIC_CLOSE,
	ZC_BACKLIGHT_OPEN,
	ZC_BACKLIGHT_CLOSE,
	ZC_WHITEBALANCE_AUTO,
	ZC_WHITEBALANCE_MANUAL,
	ZC_RGAIN_UP,
	ZC_RGAIN_DOWN,
	ZC_BGAIN_UP,
	ZC_BGAIN_DOWN,
	ZC_AEMODE_AUTO,
	ZC_AEMODE_MANUAL,
	ZC_IRIS_UP,
	ZC_IRIS_DOWN,
	ZC_GAIN_UP,
	ZC_GAIN_DOWN,
	ZC_BRIGHT_UP,
	ZC_BRIGHT_DOWN,
	ZC_SHUTTER_AUTO,
	ZC_SHUTTER_MANUAL,
	ZC_SHUTTER_UP,
	ZC_SHUTTER_DOWN,
	ZC_APERTURE_UP,
	ZC_APERTURE_DOWN,
	
	ZC_HR_ON,
	ZC_HR_OFF,
	ZC_LRREVERSE_ON,
	ZC_LRREVERSE_OFF,
	ZC_FREEZE_ON,
	ZC_FREEZE_OFF,
	ZC_PFLIP_ON,
	ZC_PFLIP_OFF,
	ZC_ICR_ON,
	ZC_ICR_OFF,
	ZC_AUTOICR_ON,
	ZC_AUTOICR_OFF,
	ZC_COLOR_UP,
	ZC_COLOR_DOWN,

}enPtzAction;

//视频传输协议类型
typedef enum
{
	MTM_TCP,
	MTM_UDP,
	MTM_MULTICAST
}enMonTranMode;

#define INIT_NET_PACK(pack,cmd)    { memset(&(pack), 0, sizeof((pack))); \
                                     (pack).head.dwMagicCode = NETWORK_MAGIC_CODE; \
                                     (pack).head.wSize = sizeof((pack)); \
									 (pack).head.wCmdId = cmd; }


#if defined(WIN32)
	#pragma pack(1)
#endif


//本协议包的帧头定义
struct _TCmdHead
{
	DWORD dwMagicCode; // Always be NETWORK_MAGIC_CODE
	WORD  wSize;       // Total packet size, including TCmdHead & Cmd Data
	WORD  wCmdId;      // command id
}__PACKED__;
typedef struct _TCmdHead TCmdHead;

//云台控制的参数
struct _TPtzActParam
{
	BYTE Param1; // 1) To Set/Get point, this means point value, range: 0 ~ 255
	             // 2) To move Left/Up/Right/Down, this means speed, range: 1 ~ 10
	             // 3) To mouse click, this means zooming ration, range: 0 ~ 99
	             
	BYTE Param2; // 1) To mouse click, this means PosX ration, range: 0~0x63, 0x80~0xE3
	
	BYTE Param3; // 1) To mouse click, this means PosY ration, range: 0~0x63, 0x80~0xE3

	// Reserved bytes, for future extension
	BYTE Reserved[29];
}__PACKED__; // Total 32 bytes
typedef struct _TPtzActParam TPtzActParam;

//云台控制的请求命令
struct _TReqPtzAct
{
	TCmdHead head;
	BYTE ChanNum;		//通道号
	enPtzAction ePtzAct;	//操作类型
	TPtzActParam ActParam;	//控制参数
}__PACKED__;
typedef struct _TReqPtzAct TReqPtzAct;

//云台控制命令的应答包
struct _TAswPtzAct
{
	TCmdHead head;
	// Client original request parameters
	BYTE ChanNum;
	enPtzAction ePtzAct;
	TPtzActParam ActParam;
	// Server answer data
	int nResult;
}__PACKED__;
typedef struct _TAswPtzAct TAswPtzAct;

//解码切换
struct _TReqDecodeSwitch
{
	TCmdHead head;			//命令头
	char EncIpAddr[IP_ADDR_LEN+1]; //编码器IP
	WORD EncPort ;				//编码器端口号
	BYTE EncChanNum ; 			//编码器通道号
	char Name[USER_NAME_LEN+1];	//用户名
	char Pswd[USER_PSW_LEN+1];	//密码
	enMonTranMode eTranMode ;	//视频传输类型
	BYTE bMain ;				//是否主码流
	BYTE DecChanNum; //解码器通道号
}__PACKED__;
typedef struct _TReqDecodeSwitch TReqDecodeSwitch;

//logout
struct _TReqLogout
{
	TCmdHead head;
}__PACKED__;
typedef struct _TReqLogout TReqLogout;

struct _TTime
{
	WORD Year;    // 1970 ~ 2100
	BYTE Month;   // 1 ~ 12
	BYTE Day;     // 1 ~ 31
	BYTE Hour;    // 0 ~ 23
	BYTE Minute;  // 0 ~ 59
	BYTE Second;  // 0 ~ 59
}__PACKED__;
typedef struct _TTime TTime;

//login
struct _TReqLogin
{
	TCmdHead head;
	WORD wClientNetVer; // Client network protocol version
	char LoginName[USER_NAME_LEN+1];
	char LoginPsw[USER_PSW_LEN+1];
	enMonTranMode eMonTranMode;
	TTime LocalTime;
	WORD wClientUdpPort; // Only valid when eMonTranMode == MTM_UDP.
	BYTE bUdpMakeHole; // when 'bUdpMakeHole' == 1, 'wClientUdpPort' will be ignored by Server.

	BYTE          Reserved[70];
}__PACKED__; // Total 128 bytes
typedef struct _TReqLogin TReqLogin;

//设备连接参数
struct _TDeviceInfo
{
	WORD 	DeviceNo ;  //用于键盘控制时的设备编号(如：摄像机编号,监视器编号)
	char 	IP[IP_ADDR_LEN+1] ;
	WORD	Port ;
	WORD	ChanNum ; 	//通道号
	char 	UserName[USER_NAME_LEN+1] ;
	char 	Password[USER_PSW_LEN+1] ;
	WORD 	DeviceType	; 	//设备类型:1-摄像机,2-监视器
}__PACKED__;
typedef struct _TDeviceInfo TDeviceInfo;


//摄像机分组
struct _TCameraGroup
{
	WORD 	GroupNo ;  //分组号
	WORD 	CameraNo ;  //摄像机号
	WORD 	PresetNo ;  //预置位编号
	WORD 	StopTime ;  //停留时间(秒)
}__PACKED__;
typedef struct _TCameraGroup CameraGroup;


#if defined(WIN32)
	#pragma pack()
#endif



#endif //__KB_DEF_H__
